//
//  LXLocation.m
//  duoduo
//
//  Created by wanghao on 16/10/11.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXLocation.h"

@interface LXLocation ()<AMapLocationManagerDelegate,AMapSearchDelegate>
{
    int count;
    BOOL hadBlock;
    CLLocationCoordinate2D coor;
    NSString * fromatAddress;
    NSDictionary * dic;
    NSString * strName;
}


@end

@implementation LXLocation

//高德定位懒加载
-(AMapLocationManager *)manager{
    if (!_manager) {
        _manager = [[AMapLocationManager alloc]init];
    }
    return _manager;
}

//创建单利
static id instance = nil;

+(id)shareDingWei
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[super alloc]init];
    });
    return instance;
}

//浅拷贝线程安全
-(id)copy
{
    return instance;
}

/**
 *  初始化
 */
-(id)init
{


    if (self = [super init]) {
        count = 0;
        hadBlock = NO;
        self.manager.delegate = self;
        //带你地理信息的一次定位
        [self.manager setDesiredAccuracy:kCLLocationAccuracyBest];
        self.manager.distanceFilter = 100;

        //在后台不被系统挂起
        [self.manager setPausesLocationUpdatesAutomatically:NO];
        // ios9以上需要
        [self.manager setAllowsBackgroundLocationUpdates:YES];
        [self.manager startUpdatingLocation];



    }
    return self;
}

/**
 *  重新定位
 */
-(void)getTheNewLocation
{

    hadBlock = NO;

    self.manager.delegate = self;
    [self.manager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.manager startUpdatingLocation];
    
}

#pragma mark -- AMapLocationManagerDelegate 高德定位代理
-  (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
{

    NSString *strLat = [NSString stringWithFormat:@"%.6f",location.coordinate.latitude];
    NSString *strLng = [NSString stringWithFormat:@"%.6f",location.coordinate.longitude];
    NSLog(@"lat--%.6f,lng--%.6f",location.coordinate.latitude,location.coordinate.longitude);

    coor = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
    count = 0;
    //    //逆地理编码
    _search = [[AMapSearchAPI alloc]init];
    _search.delegate = self;
    AMapReGeocodeSearchRequest * regeo = [[AMapReGeocodeSearchRequest alloc]init];

    regeo.location = [AMapGeoPoint locationWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];

    regeo.requireExtension = YES;

    [self.search AMapReGoecodeSearch:regeo];
    [self.manager stopUpdatingLocation];

}

//逆地理编码回调
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{

    fromatAddress = response.regeocode.formattedAddress;
    strName =response.regeocode.addressComponent.building;
    NSString * strLat = [NSString stringWithFormat:@"%.6f",response.regeocode.addressComponent.streetNumber.location.latitude];
    NSString * strLng = [NSString stringWithFormat:@"%.6f",response.regeocode.addressComponent.streetNumber.location.longitude];
    dic = @{@"City":response.regeocode.addressComponent.city,@"FormattedAddressLines":response.regeocode.formattedAddress,@"Name":strName,@"Street":response.regeocode.addressComponent.streetNumber.street,@"SubLocality":response.regeocode.addressComponent.district,@"latitude":strLat,@"longitude":strLng};

    NSLog(@"看看这个地址---%@",fromatAddress);

    if (_dingweiInfoBlock && !hadBlock) {
        hadBlock = YES;//zi sou
        LXLocationInfoModel *info = [LXLocationInfoModel new];
        info.longitude = strLng;
        info.latitude = strLat;
        info.city = response.regeocode.addressComponent.city;
        info.country = response.regeocode.formattedAddress;
        info.detailAddr = response.regeocode.formattedAddress;
        info.privoce = response.regeocode.addressComponent.province;
        info.cityCode = response.regeocode.addressComponent.citycode;
        _dingweiInfoBlock(info);
    }

    // model.placemark = place;
    // model = [CJAddressModel mj_objectWithKeyValues:dic];

    //存沙河
//    NSDictionary*dici = @{@"latitude":strLat,@"longitude":strLng,@"detailAddr":strName,@"addressDictionary":dic};
//    [def setObject:dici forKey:CJLocationDic];
//    [def synchronize];




    NSLog(@"lat--%@,lng--%@,city--%@,country--%@,detaiaddr--%@,privoce--%@",strLat,strLng,dic[@"City"],response.regeocode.formattedAddress,strName,response.regeocode.addressComponent.province);
    //dic = response.regeocode.addressComponent;

    [self.manager stopUpdatingLocation];

}

@end
